import { Controller, Get, Provide, Inject, Post, Body, Query  } from '@midwayjs/decorator';
import { Context } from '@midwayjs/koa';
import { UserService } from '../service/userService';
import { User } from '../entity/User';

@Provide()
@Controller('/')
export class HomeController {
  @Inject()
  userService: UserService;

  @Get('/user')
  async createUser(ctx: Context): Promise<void> {
    const user = await this.userService.createUser('Chen', 18);
    ctx.body = `User created: ${user.name}, age: ${user.age}`;
  }

  @Get('/users')
  async getUser(@Query('id') id: string,ctx: Context): Promise<void> {
    console.log(id,'id')
    const user = await this.userService.getUser(Number(id));
    console.log(user,'user')
    ctx.body = {
      success: true,
      message: 'User created successfully',
      data: user,
    };
  }

  @Post('/add')
  async updateAge(@Body() body: User ,ctx: Context) {
    // ...
    const { name, age } = body;
    const user = await this.userService.createUser(name, age);

    ctx.body = {
      success: true,
      message: 'User created successfully',
      data: user,
    };

  }
}